package com.mazaiting.util;

import java.util.Arrays;

/**
 * 字节工具类
 *
 * @author mazaiting
 * @since 2021/8/11
 */
public class ByteUtil {


    /**
     * byte[] 转为16进制String
     *
     * @param bytes 字节数组
     */
    public static String bytes2HexString(byte[] bytes) {
        StringBuilder ret = new StringBuilder();
        for (byte aByte : bytes) {
            String hex = Integer.toHexString(aByte & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret.append(hex.toUpperCase());
        }
        return ret.toString();
    }

    /**
     * 从一个byte[]数组中截取一部分
     *
     * @param src   源数组
     * @param begin 开始位置
     * @param count 读取数
     * @return 字节数组
     */
    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, begin + count - begin);
        return bs;
    }

    /**
     * short到字节数组的转换.
     */
    public static byte[] shortToByte(short number) {
        int temp = number;
        byte[] b = new byte[2];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8;// 向右移8位
        }
        return b;
    }

    /**
     * 字节数组到short的转换.
     */
    public static short byteToShort(byte[] b) {
        short s;
        short s0 = (short) (b[0] & 0xff);// 最高位
        short s1 = (short) (b[1] & 0xff);
        s0 <<= 8;
        s = (short) (s0 | s1);
        return s;
    }

    /**
     * int到字节数组的转换.
     */
    public static byte[] intToByte(int number) {
        int temp = number;
        byte[] b = new byte[4];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
            temp = temp >> 8;// 向右移8位
        }
        return b;
    }

    /**
     * 字节数组到int的转换.
     */
    public static int byteToInt(byte[] b) {
        int s;
        int s0 = b[0] & 0xff;// 最高位
        int s1 = b[1] & 0xff;
        int s2 = b[2] & 0xff;
        int s3 = b[3] & 0xff;
        s0 <<= 24;
        s1 <<= 16;
        s2 <<= 8;
        s = s0 | s1 | s2 | s3;
        return s;
    }

    /**
     * long类型转成byte数组
     */
    public static byte[] longToByte(long number) {
        byte[] b = new byte[8];
        // 将最低位保存在最低位 temp = temp >> 8;// 向右移8位
        Arrays.fill(b, new Long(number & 0xff).byteValue());
        return b;
    }

    /**
     * 字节数组到long的转换.
     */
    public static long byteToLong(byte[] b) {
        long s;
        long s0 = b[0] & 0xff;// 最高位
        long s1 = b[1] & 0xff;
        long s2 = b[2] & 0xff;
        long s3 = b[3] & 0xff;
        long s4 = b[4] & 0xff;// 最高位
        long s5 = b[5] & 0xff;
        long s6 = b[6] & 0xff;
        long s7 = b[7] & 0xff;

        // s0不变
        s6 <<= 8;
        s5 <<= 16;
        s4 <<= 24;
        s3 <<= 8 * 4;
        s2 <<= 8 * 5;
        s1 <<= 8 * 6;
        s0 <<= 8 * 7;
        s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
        return s;
    }

    /**
     * double到字节数组的转换.
     */
    public static byte[] doubleToByte(double num) {
        byte[] b = new byte[8];
        long l = Double.doubleToLongBits(num);
        for (int i = 0; i < 8; i++) {
            b[i] = new Long(l).byteValue();
            l = l >> 8;
        }
        return b;
    }

    /**
     * 字节数组到double的转换.
     */
    public static double byteToDouble(byte[] b) {
        long m;
        m = b[0];
        m &= 0xff;
        m |= ((long) b[6] << 8);
        m &= 0xffff;
        m |= ((long) b[5] << 16);
        m &= 0xffffff;
        m |= ((long) b[4] << 24);
        m &= 0xffffffffL;
        m |= ((long) b[3] << 32);
        m &= 0xffffffffffL;
        m |= ((long) b[2] << 40);
        m &= 0xffffffffffffL;
        m |= ((long) b[1] << 48);
        m &= 0xffffffffffffffL;
        m |= ((long) b[0] << 56);
        return Double.longBitsToDouble(m);
    }


    /**
     * float到字节数组的转换.
     */
    public static byte[] floatToByte(float x) {
        //先用 Float.floatToIntBits(f)转换成int
        int num = Float.floatToIntBits(x);
        return intToByte(num);
    }

    /**
     * 字节数组到float的转换.
     */
    public static float byteToFloat(byte[] b) {
        // 4 bytes
        int accum = 0;
        for ( int shiftBy = 2; shiftBy >= 0; shiftBy-- ) {
            accum |= (b[shiftBy] & 0xff) << shiftBy * 8;
        }
        return Float.intBitsToFloat(accum);
    }

    /**
     * char到字节数组的转换.
     */
    public static byte[] charToByte(char c){
        byte[] b = new byte[2];
        b[0] = (byte) ((c & 0xFF00) >> 8);
        b[1] = (byte) (c & 0xFF);
        return b;
    }

    /**
     * 字节数组到char的转换.
     */
    public static char byteToChar(byte[] b){
        return (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
    }

}


















